package be.kuleuven.cs.mop.domain.model.impl;


import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.junit.*;

import be.kuleuven.cs.mop.TestEnvironment;
import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.Invitation;
import be.kuleuven.cs.mop.domain.model.Task;



/**
 * Test class for the {@link InvitationManagerImpl} class
 */
public class InvitationManagerImplTest {
	
	private static Controllers		CTRL;
	private static TestEnvironment	ENV;
	
	private static InvitationManagerImpl	instance;
	private static TaskTypeImpl				myTaskType;
	
	
	
	@Before
	public void setUp() throws Exception {
		ENV		= TestEnvironment.newInstance();
		CTRL	= ENV.controllers;
		
		instance = new InvitationManagerImpl();
		
		final HashMap<UserTypeImpl, Interval> constraints = new HashMap<UserTypeImpl, Interval>();
		constraints.put(ENV.userTypes.get(1), new Interval(1, 3));
		myTaskType	= new TaskTypeImpl(ENV.world, "MyTaskType",
				new HashSet<UserTypeImpl>(ENV.world.getUserTypes()),
				constraints,
				new HashMap<String, FieldType>(),
				new HashMap<ResourceTypeImpl, Interval>());
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}
	
	@After
	public void tearDown() throws Exception {}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {}
	
	
	
	
	
	private final Controllers getControllers() { return CTRL; }
	
	@Test
	public void testConstructor() {
		new InvitationManagerImpl();
	}
	
	@Test
	public void testGetInvitations() {
		final List<InvitationImpl> list = instance.getInvitations();
		assertFalse(list == null);
		assertTrue(list.isEmpty());
	}
	
	@Test
	public void testGetInvitationsForTask() {

	}
	
	@Test
	public void testInviteValid() {
		TaskImpl task	= null;
		UserImpl user	= null;
		
		try {
			task = (TaskImpl)getControllers().getControllerTasks().createTask(
					new Schedule(ENV.datum1, ENV.datum2, 123456L),
					new HashSet<Task>(),
					new HashMap<String, Field>(),
					myTaskType);
			
			getControllers().getControllerUsers().setUser(ENV.admin);
			user = (UserImpl)getControllers().getControllerUsers().createUser(
					"John Beton", ENV.userTypes.get(1));
		} catch (final Exception e) {
			e.printStackTrace();
			assertFalse(true);
		}
		
		assertNotNull(task);
		assertNotNull(user);
		
		assertTrue(instance.getInvitations().isEmpty());
		
		try {
			instance.invite(user, task);
		} catch (final TaskManagerException e) {
			e.printStackTrace();
			assertFalse(true);
		}
		
		final List<InvitationImpl> invitations = instance.getInvitations();
		assertFalse(invitations.isEmpty());
		
		boolean found = false;
		for (final Invitation invitation : invitations) {
			if (invitation.getTask().equals(task)
					&& invitation.getUser().equals(user)) {
				found = true;
				break;
			}
		}
		assertTrue(found);
	}
	
}